package com.luo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luo.dao.AddressDao;
import com.luo.entity.QueryPageBean;
import com.luo.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zoro
 * @date 2021年09月02
 */
@Service(interfaceClass = AddressService.class)
@Transactional(readOnly = true)
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public List<Address> findAllMaps() {
        return addressDao.findAllMaps();
    }

    @Override
    public PageInfo<Address> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Address> addresses = addressDao.findPage(queryPageBean.getQueryString());
        return new PageInfo<>(addresses);
    }

    @Transactional(readOnly = false)
    @Override
    public void addAddress(Address address) {
        if (address.getLat() == null || address.getLng() == null) {
            throw new RuntimeException("数据有误！");
        }
        addressDao.addAddress(address);
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteById(Integer id) {
        addressDao.deleteById(id);
    }

    @Override
    public Map<String, Object> getAddressById(Integer id) {
        Address address = addressDao.getAddressById(id);
        Map<String, Object> map = new HashMap<>(2);
        map.put("address_detail", address.getAddressName());
        Map<String, Object> lngAndLat = new HashMap<>(2);
        lngAndLat.put("lat", address.getLat());
        lngAndLat.put("lng", address.getLng());
        map.put("userlocation", lngAndLat);
        return map;
    }


}
